package com.cskaoyan.service;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.MarketIssueExample;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author bianyonglei
 * @since 2023/05/30 22:41
 */

public class AdminIssueServiceImpl implements AdminIssueService {
    @Override
    public List<MarketIssue> list(String question, Integer page, Integer limit, String sort, String order) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
         MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();

        if (question != null) {
            criteria.andQuestionLike("%" + question + "%");
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            marketIssueExample.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        MarketIssueMapper marketIssueMapper = sqlSession.getMapper(MarketIssueMapper.class);
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);
        sqlSession.close();
        return marketIssues;
    }

    @Override
    public int create(MarketIssue marketIssue) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper marketIssueMapper = sqlSession.getMapper(MarketIssueMapper.class);
        marketIssue.setUpdateTime(new Date());
        marketIssue.setAddTime(new Date());
        marketIssue.setDeleted(false);
        marketIssueMapper.insertSelective(marketIssue);


        sqlSession.commit();
        sqlSession.close();
       return 200;
    }

    @Override
    public int update(MarketIssue marketIssue) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper marketIssueMapper = sqlSession.getMapper(MarketIssueMapper.class);
        marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        sqlSession.commit();
        sqlSession.close();
        return 200;
    }

    @Override
    public int delete(MarketIssue marketIssue) {
//        TransactionUtil.startTransaction();
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketIssueMapper marketIssueMapper = sqlSession.getMapper(MarketIssueMapper.class);

        MarketIssueExample marketIssueExample = new MarketIssueExample();
        marketIssue.setDeleted(true);
        marketIssueExample.or().andIdEqualTo(marketIssue.getId());


//        System.out.println(marketIssue.getDeleted());
//        int i = marketIssueMapper.updateByPrimaryKey(marketIssue);
        marketIssueMapper.updateByExample(marketIssue, marketIssueExample);
        sqlSession.commit();
        sqlSession.close();

        return 200;
    }
}
